package com.transcargo.rating;

import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.geo.GeoObjectNotFoundException;
import com.transcargo.users.User;
import com.transcargo.users.UserNotFoundException;
import com.transcargo.util.Normalizer;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Date: 24.04.2011
 * Time: 16:15:32
 */
public class Feedback {
    private int Id = 0;
    private int User_Id = 0;
    private String date = "";
    private int rating = 0;
    private String feedback = "";
    private User author = null;
    public Feedback(int id, db dbase) {
        Id = id;
        try {
            ArrayList<String[]> list = dbase.execute("SELECT feedbacks.user_id, feedbacks.author, feedbacks.date, feedbacks.rating, feedbacks.feedback, registered_users.organization FROM feedbacks,registered_users WHERE feedbacks.id=" + id + " and registered_users.id=feedbacks.author");
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter.hasNext()) {
                    String[] ctr = iter.next();
                    User_Id = Integer.parseInt(ctr[0]);
                    int author_id = Integer.parseInt(ctr[1]);
                    date = Normalizer.DenormalizeTime(ctr[2]);
                    rating = Integer.parseInt(ctr[3]);
                    feedback = ctr[4];
                    author = new User(author_id, dbase);
                }
            }
        } catch (dbException e) {
            System.out.println("Feedback exception! Unable to find feedback id " + id);
        } catch (UserNotFoundException e) {
            System.out.println("Feedback exception! User not found: " + User_Id);
		} catch (GeoObjectNotFoundException e) {
            System.out.println("Feedback exception! Geo location not found");
		}
    }
    public int getId() {
        return Id;
    }
    public int getUserId() {
        return User_Id;
    }
    public User getAuthor() {
        return author;
    }
    public String getDate() {
        return date;
    }
    public int getRating() {
        return rating;
    }
    public String getFeedback() {
        return feedback;
    }
}
